package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.dto.DishDto;
import com.itheima.result.R;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
@EnableTransactionManagement
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dto){
        log.info(dto.toString());
        dishService.saveWithDishFlavor(dto);
        return R.success("新增成功");
    }

    /**
     * 分页查询所有菜品
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R page(int page,int pageSize,String name){
        Page pageResult = dishService.selectByPage(page, pageSize, name);
        return R.success(pageResult);
    }

    /**
     * 点击修改菜品时，数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectByid(@PathVariable long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }


    /**
     * 修改菜品：保存
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    /**
     * 启售，批量起售，停售，批量停售
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R soldAndStop(@RequestParam List<Long> ids,Integer status){
        dishService.soldAndStop(ids);
        return R.success("修改成功");
    }

    /**
     * 删除+批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R remove(@RequestParam List<Long> ids){
        dishService.MyRemove(ids);
        return R.success("删除成功");
    }


    /**
     * 套餐管理中，点击新建套餐时，会触发钩子函数，查询 分类对应的菜品
     * @param dish
     * @return
     * 追加：在手机APP中，登录后，触发的函数，需要把菜品的口味也查出来，一起返回给前端
     */
    @GetMapping("/list")
    public R list(Dish dish){
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,dish.getCategoryId());
        lqw.eq(Dish::getStatus,1);
        //list中就是该分类对应的所有的菜品信息
        List<Dish> list = dishService.list(lqw);

        List<DishDto> result = new ArrayList<>();
        for (Dish dish2 : list) {
            //dishdto,用来装载最终需要返回的数据
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish2,dishDto);

            //通过dishid，查询口味关联表
            LambdaQueryWrapper<DishFlavor> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(DishFlavor::getDishId,dish2.getId());
            List<DishFlavor> list2 = dishFlavorService.list(lqw2);

            dishDto.setFlavors(list2);
            result.add(dishDto);
        }
        return R.success(result);
    }
}
